<?php
$product_type=array('index_hot','index_new','index_best');
if(!empty($_POST['submit'])&&$_POST["submit"]==lang_show('submit'))
{
	unset($_POST['submit']);
	foreach ($product_type as $item){
		$best = array();
		$info_ids = "";
		if (!empty($_POST[$item])){
		    $sql = "select id from ".BESTPRODUCT." where type='{$item}'";
			$db->query($sql);
			$bestId = $db->fetchField('id');
			if($bestId>0){
				$sql = "update ".BESTPRODUCT." set `info_ids`='{$_POST[$item]}',`create_user`='{$_SESSION['ADMIN_USER']}' where id={$bestId}";
			}else{
				$sql = "insert into ".BESTPRODUCT." (`type`,`info_ids`,`create_user`) values ('{$item}','{$_POST[$item]}','{$_SESSION['ADMIN_USER']}')";
			}
			$db->query($sql);
		}else{
		    $sql = "select id from ".BESTPRODUCT." where type='{$item}'";
		    $db->query($sql);
		    $bestId = $db->fetchField('id');
		    if($bestId>0){
    		    $sql = "update ".BESTPRODUCT." set `info_ids`='{$_POST[$item]}',`create_user`='{$_SESSION['ADMIN_USER']}' where id={$bestId}";
    		    $db->query($sql);
		    }
		}
	}
	admin_msg("module.php?m=activity&s=best_product.php","设置成功");
	exit;
}elseif (!empty($_GET['info_ids'])&&in_array($_GET['type'],$product_type)){
	$sql="select a.id,a.pic,a.price,a.pname,b.product_title,b.pic AS best_pic from ".PRO." as a left join ".BESTPRODUCTLIST." as b on a.id=b.info_id  where a.id in ({$_GET['info_ids']}) order by FIND_IN_SET(a.id,'{$_GET['info_ids']}') ASC";
	$db->query($sql);
	$re=$db->getRows();
	echo json_encode($re);
	exit;
}

$sql = "select info_ids,type FROM ".BESTPRODUCT;
$db->query($sql);
foreach ($re=$db->getRows() as $item){
	$info[$item['type']]=$item['info_ids'];
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>热销商品</title>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<link href="main.css" rel="stylesheet" type="text/css" />
<script src="../script/jquery-1.4.4.min.js" type=text/javascript></script>
<script src="../script/my_lightbox.js" language="javascript"></script>
</head>
<body>
<div class="bigbox">
  <div class="bigboxhead">热卖商品</div>
  <div class="bigboxbody">
    <form name="sysconfig" action="" method="post">
      <table width="100%" cellspacing="0">
        <tr class="theader">
          <td height="23" colspan="4" >热卖商品设置</td>
        </tr>
        <tr>
          <td align="left">热卖商品</td>
          <td colspan="3" align="left" ><input class="text" name="index_best" type="text" id="index_best" value="<?php echo $info['index_best'];?>" />
            <span class="bz">填写产品ID号，用,号分隔。如：　1,2,3</span></td>
        </tr>
        <tr>
        <td colspan="3" id="index_best_span" style="display:none;">
        </td>
        </tr>
        
        <tr>
          <td align="left">新品上架</td>
          <td colspan="3" align="left" ><input class="text" name="index_new" type="text" id="index_new" value="<?php echo $info['index_new'];?>" />
            <span class="bz">填写产品ID号，用,号分隔。如：　1,2,3</span></td>
        </tr>
        <tr>
        <td colspan="3" id="index_new_span" style="display:none;">
        </td>
        </tr>
        <tr>
          <td align="left">疯狂抢购</td>
          <td colspan="3" align="left" ><input class="text" name="index_hot" type="text" id="index_hot" value="<?php echo $info['index_hot'];?>" />
            <span class="bz">填写产品ID号，用,号分隔。如：　1,2,3</span></td>
        </tr>
        <tr>
        <td colspan="3" id="index_hot_span" style="display:none;">
        
        
        </td>
        </tr>
        <tr>
        
          <td width="17%" height="40" align="right">&nbsp;</td>
          <td colspan="3" align="left" ><input  class="btn" type="submit" name="submit" value="<?php echo lang_show('submit');?>"></td>
        </tr>
      </table>
    </form>
  </div>
</div>
<script>
$('input[id^=index]').bind('click change',function(){
	var $$=$(this);
	//alert($$.val());

	if(!$$.val()){return false;}


	if(/[^\d,]/.test($$.val())){

		alert('id请使用半角,分隔');
		return false;

	}

	$.getJSON('?m=activity&s=best_product.php&type='+$$.attr('id')+'&info_ids='+$$.val(),function(data){
			var str='<table width="100%" class="alc"><tbody><tr><th width="100" align="left">产品图</th><th width="350">产品名称</th><th width="200" align="left">短标题</th><th>图片</th><th width="100">排序</th></tr>';
				$.each(data,function(k,v){
					str+='<tr><td><img width="50" src="'+v.pic+'"></td>';
					str+='<td class="al"><input type="hidden" name="'+$$.attr('id')+'_id[]" value="'+v.id+'"><div class="name">'+v.pname+'</div></td>';
				  str+='<td><div class="name"><input type="text" value="'+(v.product_title==null?'':v.product_title)+'" name="'+$$.attr('id')+'_title[]"/></div></td>';
				  str+='<td><input class="text" type="text" name="'+$$.attr('id')+'_pic[]" id="pic_'+v.id+'" msg="请上传图片" value="'+(v.best_pic==null?'':v.best_pic)+'" />[<a href="javascript:uploadfile(\'上传图片\',\'pic_'+v.id+'\',0,0,\'product/best\')">上传</a>] [<a href="javascript:preview(\'pic_'+v.id+'\');">预览</a>] [<a onclick="javascript:$(\'#pic_'+v.id+'\').val(\'\');" href="#">删除</a>] </td>';
				  str+='<td align="center"><input type="text" style="width:30px;" name="'+$$.attr('id')+'_displayorder[]" value="'+(k+1)+'"></td>';
					});
		      
       str+='</tbody></table>';

       $('#'+$$.attr('id')+'_span').html(str).show();


		



		})


	
})



</script>
</body>
</html>
